package com.wzq.app.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.cb.ratingbar.CBRatingBar;
import com.wzq.app.R;
import com.wzq.app.adapter.RatingAdapter;
import com.wzq.app.api.Api;
import com.wzq.app.api.ApiResponseCode;
import com.wzq.app.base.BaseActivity;
import com.wzq.app.constant.App;
import com.wzq.app.manager.UserManager;
import com.wzq.app.model.BaseModel;
import com.wzq.app.model.RateModel;
import com.wzq.app.model.Result;
import com.wzq.app.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @ProjectName: wzq
 * @Package: com.wzq.app.ui
 * @ClassName: RatingActivity
 * @Version: 1.0
 */
public class RatingActivity extends BaseActivity {

    private static final String TAG = "RatingActivity";


    List<RateModel> list = new ArrayList<>();

    int[] res = new int[5];
    @BindView(R.id.head_left)
    TextView headLeft;
    @BindView(R.id.head_right_text)
    TextView headRightText;
    @BindView(R.id.review)
    EditText review;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.head_left_text)
    TextView headLeftText;

    @Override
    public int getLayoutId() {
        return R.layout.rating_activity;
    }

    String type = "-1";
    int uid = -1;
    String name = "";

    RatingAdapter ratingAdapter;
    Map<Integer, Result> resultMap = new HashMap<>();
    List<Result> resultList = new ArrayList<>();

    @Override
    public void configView() {
        type = getIntent().getStringExtra("type");
        uid = getIntent().getIntExtra("uid", -1);
        name = getIntent().getStringExtra("name");
        if (App.TEACHER.equals(type)) {
            headLeftText.setText("评价老师");
        }
        if (App.STUDENT.equals(type)) {
            headLeftText.setText("评价学生");
        }
        headLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        headRightText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.showToast(mContext, "提交评价");
                Log.d(TAG, "答题数目：" + resultList.size());
                for (int i = 0; i < resultList.size(); i++) {
                    Log.d(TAG, "选项" + resultList.get(i).getIndex() + "打了" + resultList.get(i).getValue() + "分");
                }
                ListSort();
                if (resultList.size() == list.size()) {
                    save();
                } else {
                    ToastUtils.showToast(mContext, "请完成所有评价再提交");
                }
            }
        });
    }

    private void save() {
        Map<String, Object> map = new HashMap<>();
        Log.d(TAG, "result:" + JSON.toJSONString(resultList));
        map.put("arrayObj", JSON.toJSONString(resultList));
        map.put("mark", review.getText().toString().trim());
        map.put("id", uid);
        map.put("currentId", UserManager.getInstance().getUser().getId());
        map.put("name", name);
        map.put("type", type);
        Call<BaseModel<String>> call = Api.getInstance().service.save(map);
        call.enqueue(new Callback<BaseModel<String>>() {
            @Override
            public void onResponse(Call<BaseModel<String>> call, Response<BaseModel<String>> response) {
                if (response.isSuccessful()) {
                    Log.d(TAG, response.toString());
                    BaseModel<String> model = response.body();
                    if (model.code == ApiResponseCode.RESPHONE_OK) {
                        ToastUtils.showToast(mContext, "评价成功");
                        finish();
                    } else {
                        ToastUtils.showToast(mContext, "评价失败");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseModel<String>> call, Throwable t) {
                ToastUtils.showToast(mContext, "评价失败");
            }
        });
    }

    private void initViews() {
        ratingAdapter = new RatingAdapter(mContext, list);
        listview.setAdapter(ratingAdapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CBRatingBar cbRatingBar = (CBRatingBar) view.findViewById(R.id.rating1);
                TextView textView = view.findViewById(R.id.rating1_text);
                cbRatingBar.setOnStarTouchListener(new CBRatingBar.OnStarTouchListener() {
                    @Override
                    public void onStarTouch(int i) {
                        Result result = new Result();

                        switch (i) {
                            case 1:
                                result.setIndex(3);
                                result.setValue(list.get(position).getGoalD());
                                textView.setText(list.get(position).getEvaD());
                                Log.d(TAG, "评价：第" + (position + 1) + "题，得分=" + i + ",评价内容：" + list.get(position).getEvaD());
                                break;
                            case 2:
                                result.setIndex(2);
                                result.setValue(list.get(position).getGoalC());
                                textView.setText(list.get(position).getEvaC());
                                Log.d(TAG, "评价：第" + (position + 1) + "题，得分=" + i + ",评价内容：" + list.get(position).getEvaC());
                                break;
                            case 3:
                                result.setIndex(1);
                                result.setValue(list.get(position).getGoalB());
                                textView.setText(list.get(position).getEvaB());
                                Log.d(TAG, "评价：第" + (position + 1) + "题，得分=" + i + ",评价内容：" + list.get(position).getEvaB());
                                break;
                            case 4:
                                result.setIndex(0);
                                result.setValue(list.get(position).getGoalA());
                                textView.setText(list.get(position).getEvaA());
                                Log.d(TAG, "评价：第" + (position + 1) + "题，得分=" + i + ",评价内容：" + list.get(position).getEvaA());
                                break;
                            default:
                                break;
                        }
                        resultMap.put(position, result);
                    }
                });
            }
        });
    }

    private void ListSort() {
        for (int i = 0; i < resultMap.size(); i++) {
            resultList.add(resultMap.get(i));
        }
    }

    @Override
    public void requestData() {
        Call<BaseModel<List<RateModel>>> call = Api.getInstance().service.getRateList(type);
        call.enqueue(new Callback<BaseModel<List<RateModel>>>() {
            @Override
            public void onResponse(Call<BaseModel<List<RateModel>>> call, Response<BaseModel<List<RateModel>>> response) {
                if (response.isSuccessful()) {
                    BaseModel<List<RateModel>> baseModel = response.body();
                    if (baseModel.code == ApiResponseCode.RESPHONE_OK) {
                        list.addAll(baseModel.obj);
                        initViews();
                    } else {
                        ToastUtils.showToast(mContext, "获取评分方式失败，请检查网络");
                    }

                }
            }

            @Override
            public void onFailure(Call<BaseModel<List<RateModel>>> call, Throwable t) {
                ToastUtils.showToast(mContext, "获取评分方式失败，请检查网络");
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
